<template>
  <el-tree
    ref="tree"
    :data="data"
    node-key="value"
    :props="defaultProps"
    :default-expand-all="true"
    :render-after-expand="true"
    :highlight-current="true"
    :expand-on-click-node="false"
    @node-click="handleNodeClick"
  />
</template>

<script>
import { fetchSubUserAuthOrgArray } from '@/api/base/common'

export default {
  name: 'OrgTreeComponent',
  props: {
    // 1、父组件传来的值
    orgcode: {
      type: String,
      default: '',
      require: true
    }
  },
  data() {
    // const _this = this
    return {
      data: null,
      dataLoading: false,
      defaultProps: {
        children: 'children',
        label: 'label'
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.dataLoading = true
      fetchSubUserAuthOrgArray().then(response => {
        this.data = response.data
        this.dataLoading = false

        this.$nextTick(() => {
          // 高亮显示节点
          this.$refs.tree.setCurrentKey(this.orgcode)
        })
      }, (response) => {
        this.dataLoading = false
      })
    },
    handleNodeClick(data, node, other) {
      this.$emit('update:orgcode', data.value)
    }
  }
}
</script>
